package trader.discovery;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;

import trader.util.Util;

public class Search {
    private static final double PRICE_LIMIT = 3;
    public static void main(String[] args) {
        new Search().run();
    }
    
    public void run() {
        String alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        for (int i = 0; i < alphabet.length(); ++i) {
            for (int j = 0; j < alphabet.length(); ++j) {
                for (int k = 0; k < alphabet.length(); ++k) {
                    String id = "" + alphabet.charAt(i) + alphabet.charAt(j) + alphabet.charAt(k);
                    query(id);
                }
            }
        }
    }
    
    public void query(String id) {
        try {
            URL url = new URL(Util.BASE_URL + "/detailed_data.php?id=" + id);
            HttpURLConnection conn = (HttpURLConnection)url.openConnection();
            conn.setRequestMethod("GET");
            String line = "";
            String magic = "<td width=\"300\" class=\"heading_14\">";
            BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
            while ((line = in.readLine()) != null) {
                line = line.trim();
                if (line.equals("")) {
                    continue;
                }
                if (line.startsWith(magic) && line.substring(magic.length()).length() > 5) {
                    checkPrice(id);
                    return;
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
            System.err.println("SKIPPED: " + id);
        }
    }
    
    public void checkPrice(String id) throws IOException {
        URL url = new URL(Util.BASE_URL + "/scripts/php/data/getdata.php");
        HttpURLConnection conn = (HttpURLConnection)url.openConnection();
        String data = "action=data_by_id&id=" + id;
        conn.setRequestMethod("POST");
        conn.setDoOutput(true);
        OutputStream out = conn.getOutputStream();
        out.write(data.getBytes());
        out.close();
        String line = "";
        BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
        while ((line = in.readLine()) != null) {
            if (line.equals("")) {
                continue;
            }
            JSONObject obj = (JSONObject)Util.fromJSon(line);
            JSONArray instruments = (JSONArray)obj.get("instruments");
            if  (instruments.isEmpty()) {
                continue;
            }
            if (instruments.size() != 1) {
                System.err.println(id + " length= " + instruments.size());
            }
            JSONObject instr = (JSONObject)instruments.get(0);
            double ask = 0;
            if (instr.get("ask") instanceof Long) {
                ask = (double)(Long)instr.get("ask");
            } else if (instr.get("ask") instanceof Double) {
                ask = (Double)instr.get("ask");
            }
            if (ask != 0 && ask >= PRICE_LIMIT) {
                System.out.print("\", \"" + id);
            }
        }
    }
}
